package Persistence;

import java.sql.ResultSet;

/**
 *
 * @author kenneth
 */
public class UserMapper {
    
    private DatabaseConnection dbConn;
    
    public UserMapper(DatabaseConnection dbConn) {
          this.dbConn = dbConn;
    }
    
    public boolean authenticateUser(String email, String password) {
        boolean userExists = false; 
        dbConn.openConnection();
        try {   
            ResultSet res = dbConn.executeQuery("SELECT email FROM users WHERE email = '" + email + "' AND paswoord = '" + password + "';");
            if(res.next())
                userExists = true;
        } catch (Exception e) {
            e.printStackTrace();
        }    
        dbConn.closeConnection();
        return userExists;
    }
    
    public boolean registerUser(String email, String password) {
        boolean registerSuccess = false;
        dbConn.openConnection();
        try {
            ResultSet res = dbConn.executeQuery("SELECT email FROM users WHERE email = '" + email + "'");
            if(res.next())
                return false;
            dbConn.executeUpdate("INSERT INTO users (email, paswoord) VALUES ('" + email + "','" + password + "')");
        } catch (Exception e) {
            e.printStackTrace();
        }
        dbConn.closeConnection();
        return true;
    }
    
    public boolean resetPasswordFromUser(String email) {
        return false;
    }
    
    /*
    public int getIdByEmail(String email) {
        int id = 0;
        dbConn.openConnection();
        try {
            ResultSet res = dbConn.executeQuery("SELECT id FROM users WHERE email = '" + email + "';");
            if(res.next()) {
                id = res.getInt("id");
            }
            
        } catch (SQLException ex) {
            Logger.getLogger(UserMapper.class.getName()).log(Level.SEVERE, null, ex);
        }
        return id;
    }
    */
}
